package com.nbsaas.boot.plugin.http;

import com.nbsaas.boot.plugin.Plugin;
import com.nbsaas.boot.plugin.PluginContext;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * HTTP客户端插件
 * 支持HTTP/HTTPS请求
 */
public class HttpClientPlugin implements Plugin {
    private PluginContext context;
    private HttpClient httpClient;
    private final Map<String, String> defaultHeaders = new ConcurrentHashMap<>();
    
    @Override
    public String getId() {
        return "http-client-plugin";
    }
    
    @Override
    public String getName() {
        return "HTTP客户端插件";
    }
    
    @Override
    public String getVersion() {
        return "1.0.0";
    }
    
    @Override
    public String getDescription() {
        return "提供HTTP/HTTPS请求功能，支持GET、POST等方法";
    }
    
    @Override
    public void init(PluginContext context) {
        this.context = context;
        System.out.println("HTTP客户端插件初始化: " + getName());
        
        // 创建HTTP客户端
        httpClient = HttpClient.newBuilder()
            .connectTimeout(Duration.ofSeconds(10))
            .build();
            
        // 设置默认请求头
        defaultHeaders.put("User-Agent", "HttpClientPlugin/1.0");
        defaultHeaders.put("Accept", "*/*");
    }
    
    @Override
    public void start() {
        System.out.println("HTTP客户端插件启动: " + getName());
    }
    
    @Override
    public void stop() {
        System.out.println("HTTP客户端插件停止: " + getName());
    }
    
    /**
     * 发送GET请求
     * @param url 请求URL
     * @return 响应结果
     */
    public HttpResponse<String> get(String url) throws IOException, InterruptedException {
        return get(url, null);
    }
    
    /**
     * 发送GET请求
     * @param url 请求URL
     * @param headers 请求头
     * @return 响应结果
     */
    public HttpResponse<String> get(String url, Map<String, String> headers) throws IOException, InterruptedException {
        HttpRequest.Builder requestBuilder = HttpRequest.newBuilder()
            .uri(URI.create(url))
            .GET();
            
        addHeaders(requestBuilder, headers);
        
        return httpClient.send(requestBuilder.build(), HttpResponse.BodyHandlers.ofString());
    }
    
    /**
     * 发送POST请求
     * @param url 请求URL
     * @param body 请求体
     * @return 响应结果
     */
    public HttpResponse<String> post(String url, String body) throws IOException, InterruptedException {
        return post(url, body, null);
    }
    
    /**
     * 发送POST请求
     * @param url 请求URL
     * @param body 请求体
     * @param headers 请求头
     * @return 响应结果
     */
    public HttpResponse<String> post(String url, String body, Map<String, String> headers) throws IOException, InterruptedException {
        HttpRequest.Builder requestBuilder = HttpRequest.newBuilder()
            .uri(URI.create(url))
            .POST(HttpRequest.BodyPublishers.ofString(body));
            
        addHeaders(requestBuilder, headers);
        
        return httpClient.send(requestBuilder.build(), HttpResponse.BodyHandlers.ofString());
    }
    
    /**
     * 发送PUT请求
     * @param url 请求URL
     * @param body 请求体
     * @return 响应结果
     */
    public HttpResponse<String> put(String url, String body) throws IOException, InterruptedException {
        return put(url, body, null);
    }
    
    /**
     * 发送PUT请求
     * @param url 请求URL
     * @param body 请求体
     * @param headers 请求头
     * @return 响应结果
     */
    public HttpResponse<String> put(String url, String body, Map<String, String> headers) throws IOException, InterruptedException {
        HttpRequest.Builder requestBuilder = HttpRequest.newBuilder()
            .uri(URI.create(url))
            .PUT(HttpRequest.BodyPublishers.ofString(body));
            
        addHeaders(requestBuilder, headers);
        
        return httpClient.send(requestBuilder.build(), HttpResponse.BodyHandlers.ofString());
    }
    
    /**
     * 发送DELETE请求
     * @param url 请求URL
     * @return 响应结果
     */
    public HttpResponse<String> delete(String url) throws IOException, InterruptedException {
        return delete(url, null);
    }
    
    /**
     * 发送DELETE请求
     * @param url 请求URL
     * @param headers 请求头
     * @return 响应结果
     */
    public HttpResponse<String> delete(String url, Map<String, String> headers) throws IOException, InterruptedException {
        HttpRequest.Builder requestBuilder = HttpRequest.newBuilder()
            .uri(URI.create(url))
            .DELETE();
            
        addHeaders(requestBuilder, headers);
        
        return httpClient.send(requestBuilder.build(), HttpResponse.BodyHandlers.ofString());
    }
    
    private void addHeaders(HttpRequest.Builder builder, Map<String, String> headers) {
        // 添加默认请求头
        defaultHeaders.forEach(builder::header);
        
        // 添加自定义请求头
        if (headers != null) {
            headers.forEach(builder::header);
        }
    }
} 